package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcAdministrative;
import com.csun.cmny.provider.model.domain.OmcAppraiser;
import com.csun.cmny.provider.model.dto.AdministrativeSaveDto;
import com.csun.cmny.provider.model.dto.AdministrativeUpdateDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserSaveDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcAdministrativeService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/administrative")
public class OmcAdministrativeController extends BaseController {

    @Resource
    private OmcAdministrativeService administrativeService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;



    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增科室")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid AdministrativeSaveDto appraiserSaveDto) {

        OmcAdministrative omcAdministrative = new OmcAdministrative();
        BeanUtils.copyProperties(appraiserSaveDto,omcAdministrative);
        omcAdministrative.setGroupId(getLoginAuthDto().getGroupId());
        omcAdministrative.setOperator(getLoginAuthDto().getUserName());
        omcAdministrative.setUserId(getLoginAuthDto().getUserId());
        int save = administrativeService.save(omcAdministrative);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改科室")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid AdministrativeUpdateDto administrativeUpdateDto) {

        OmcAdministrative omcAdministrative = new OmcAdministrative();
        BeanUtils.copyProperties(administrativeUpdateDto,omcAdministrative);
        int update = administrativeService.update(omcAdministrative);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除科室")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper delete(Long id) {

        int delete = administrativeService.deleteByKey(id);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询科室")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper update() {

        OmcAdministrative omcAdministrative = new OmcAdministrative();
        omcAdministrative.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcAdministrative> select = administrativeService.select(omcAdministrative);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),select);
    }
}
